package priv.pront.code.national.acwing.graph;

import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-02 12:22
 */
public class AcWing859_Kruskal算法求最小生成树 {

    static int n, m;
    static int[] p;
    static Edge[] edges;

    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt(); m = scanner.nextInt();
        edges = new Edge[m + 10];
        p = new int[m + 10];
        for(int i = 0; i < m; i++){
            edges[i] = new Edge(scanner.nextInt(), scanner.nextInt(), scanner.nextInt());
        }
        Arrays.sort(edges, 0, m);
        int t = kruskal();
        System.out.println(t == -1 ? "impossible" : t);
    }

    private static int find(int x){
        if(p[x] != x) p[x] = find(p[x]);
        return p[x];
    }

    private static int kruskal(){
        // 初始化并查集
        for(int i =0; i < m; i++) p[i] = i;
        int res = 0, cnt = 0;
        for(int i = 0; i < m; i++){
            int a = edges[i].a;
            int b = edges[i].b;
            int c = edges[i].c;
            a = find(a);
            b = find(b);
            if(a != b){
                p[a] = b;
                res += c;
                cnt++;
            }
        }
        if(cnt < n - 1) return -1;
        return res;
    }


    public static class Edge implements Comparable<Edge>{
        public int a;
        public int b;
        public int c;

        public Edge(int a, int b, int c){
            this.a = a;
            this.b = b;
            this.c = c;
        }

        @Override
        public int compareTo(Edge o) {
            return Integer.compare(c, o.c);
        }
    }
}
